package edu.visualization.homework.entity;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {

	private TreeNode parent;
	private List<TreeNode> children;

	private String value;
	private double x;
	private double y;

	private double p;
	private double beginAngle;
	private double endAngle;

	public TreeNode(String value, TreeNode parent) {
		this.value = value;
		this.parent = parent;
	}

	public String getValue() {
		return value;
	}

	public void setParent(TreeNode parent) {
		this.parent = parent;
	}

	public TreeNode getParent() {
		return parent;
	}

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}

	public void setX(double x) {
		this.x = x;
	}

	public void setY(double y) {
		this.y = y;
	}

	public double getP() {
		return p;
	}

	public void setP(double p) {
		this.p = p;
	}

	public double getBeginAngle() {
		return beginAngle;
	}

	public void setBeginAngle(double beginAngle) {
		this.beginAngle = beginAngle;
	}

	public double getEndAngle() {
		return endAngle;
	}

	public double getMidAngle() {
		return (beginAngle + endAngle) / 2;
	}

	public void setEndAngle(double endAngle) {
		this.endAngle = endAngle;
	}

	public void addChild(TreeNode node) {
		if (children == null) {
			children = new ArrayList<>();
		}
		children.add(node);
	}

	/**
	 * 获得节点的index：从0到size-1
	 * 
	 * @param node
	 * @return
	 */
	public int getIndex(TreeNode node) {
		return children.indexOf(node);
	}

	public int getSizeOfChildren() {
		return children.size();
	}

	public List<TreeNode> getChildren() {
		return children;
	}

	public double getParentX() {
		double sum = 0;
		for (TreeNode node : children) {
			sum = sum + node.getX();
		}
		return sum / children.size();
	}

	public String toString() {
		return "value:" + value + " y:" + y + " p:" + p + " leaves:" + numOfLeaves();
	}

	/**
	 * 判断是不是叶子节点
	 * 
	 * @return
	 */
	public boolean isLeaf() {
		return children == null;
	}

	/**
	 * 获得对应树中叶子节点的个数
	 * 
	 * @return
	 */
	public int numOfLeaves() {
		if (isLeaf()) {
			return 1;
		} else {
			int sum = 0;
			for (TreeNode node : children) {
				sum = sum + node.numOfLeaves();
			}
			return sum;
		}
	}

	public int getDepth() {
		
		if (this.getChildren() == null) {
			return 1;
		} else {
			int max = 0;
			for (TreeNode node : this.getChildren()) {
				max = Math.max(max, node.getDepth());
			}
			return max + 1;
		}
	}
}
